﻿// This code is released under the BSD license.
namespace PugLib.UnitTest
{
    using System;
    using System.Reflection;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TestCollateral;

    [TestClass]
    public class ReflectTest
    {
        [TestMethod]
        public void GetPropertyTest()
        {
            PropertyInfo dayProperty = Reflect.GetProperty<DateTime, int>(dt => dt.Day);
            Assert.AreEqual("Day", dayProperty.Name);

            dayProperty = Reflect.GetProperty<DateTime>(dt => dt.Day);
            Assert.AreEqual("Day", dayProperty.Name);

            PropertyInfo info = Reflect.GetProperty<Computer>(c => c.Domain);
            Assert.AreEqual("Domain", info.Name);
        }

        [TestMethod]
        public void GetTest()
        {
            Computer compy = new Computer { Domain = "test", Number = 17 };
            Type t = compy.GetType();
            const string fieldName = "Domain";
            const string expected = "test";
            object actual = Reflect.Get(t, fieldName, compy);
            Assert.AreEqual(expected, actual);

            actual = Reflect.Get(fieldName, compy);
            Assert.AreEqual(expected, actual);

            actual = compy.Get(compy.GetType(), fieldName + "invalid");
            Assert.IsNull(actual);

            string actualString = Reflect.GetString(fieldName, compy);
            Assert.AreEqual(expected, actualString);

            const int expectedNumber = 17;
            int actualNumber = Reflect.GetAndConvert<Computer, int>("Number", compy);
            Assert.AreEqual(expectedNumber, actualNumber);
        }

        [TestMethod]
        public void GetMethodTest()
        {
            MethodInfo info = Reflect.GetMethod<Computer>(c => c.IsAServer());
            Assert.AreEqual(info.Name, "IsAServer");
        }
    }
}